var myInputComponent = React.createClass({
    regList: {
        name: /^([\w\u4E00-\u9FA5_\-]+)+$/,
        onlyname:/^([\w\u4E00-\u9FA5_\-\s]+)+$/,
        code: /^([\w_\-]+)+$/,
        phone: /^([\d_\-\+]+)+$/,
        number: /^\d+(\.\d+)?$/,
        hsCode: /^\d+$|^\d+\.\d+$/,
        positivenum: /^[1-9](\d)*$/,
        packageNum: /^[1-9]\d*$/,
        isLimitFloat:/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,3})?$/,
        email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
        triadCode: /^[A-Za-z]+$/
    },
    resetInput: function() {

    },
    setMaxLength: function(rule){
        this.props.rule = rule;
        this.setState();
    },
    showErrorMsg: function(msg) {
        var self = this;
        this.props.required = "required";
        if(msg) {
            this.props.msg = msg;
        } else{
            this.props.msg = '';
        }
        this.props.error = "error-msg";
        this.setState();
    },
    hideErrorMsg: function() {
        this.props.required = "";
        this.props.msg = "";
        this.props.error = "";
        this.setState();
    },
    isOk: function() {
        var rule = null;
        var errorMsg = null;
        if(!this.props.value) {
            this.props.value = "";
        }
        if(this.props.errorMsg) {
            this.props.errorMsg = this.props.errorMsg.replaceAll("'", '"');
            errorMsg = eval('(' + this.props.errorMsg + ')');
        }
        if(this.props.rule) {
            this.props.rule = this.props.rule.replaceAll("'", '"');
            rule = eval('(' + this.props.rule + ')');
        }
        this.isValidate = this.check(this.props.value, rule, errorMsg);
        return this.isValidate;
    },
    isValidate: false,
    componentDidMount: function() {
        if(this.props.readOnly || this.props.readonly) {
            $(this.refs[this.props.refId]).attr("readonly", "readonly");
        }
        this.refs[this.props.refId] = this;
    },
    receiveMsg: function(data) {
        this.props.required = "";
        this.props.msg = "";
        this.props.error = "";
    },
    check: function(val, rule, errorMsg) {
        var reg;
        var maxLength;
        var minLength;
        var require;
        this.props.value = val;
        if(rule && rule.isUperCase == "true") {
            val = val.toUpperCase();
        }
        if(rule && rule.maxLength) {
            if(val.length >= parseInt(rule.maxLength)) {
                this.props.value = val.substring(0, parseInt(rule.maxLength));
                val = val.substring(0, parseInt(rule.maxLength));
            } else {
                this.props.value = val;
            }
            this.setState();
        }
        if(rule && rule.require) {
            this.props.msg = "";
            this.props.error = "";
            if(!$.trim(val)) {//空
                this.props.required = "required";
                this.setState();
                return false;
            } else {//非空
                this.props.required = "";
                this.setState();
            }
        }else{
            if(!$.trim(val)) {//空
                this.props.msg = "";
                this.props.error = "";
                this.setState();
                return false;
            }
        }
        if(rule && rule.reg && $.trim(val)) {
            var reg = "";
            if(this.regList[rule.reg]) {
                reg = this.regList[rule.reg];
            } else {
                reg = eval('(' + rule.reg + ')');
            }
            if(!reg.test(val)) {
                this.showErrorMsg(errorMsg.rule);
                return false;
            } else {
                this.hideErrorMsg();
            }
        }
        return true;
    },
    change: function(ev) {

        var val = ev.target.value;
        var rule = null;
        var errorMsg = null;

        if(this.props.errorMsg) {
            this.props.errorMsg = this.props.errorMsg.replaceAll("'", '"');
            errorMsg = eval('(' + this.props.errorMsg + ')');
        }
        if(this.props.rule) {
            this.props.rule = this.props.rule.replaceAll("'", '"');
            rule = eval('(' + this.props.rule + ')');
        }
        this.isValidate = this.check(val, rule, errorMsg);
        this.setState();
    },
    getInitialState: function () {
        this.props.required = "";
        this.props.msg = "";
        this.props.error = "";
        return this.props ? this.props : {};
    },
    componentWillReceiveProps:function(nextProps){

        if(this.props.msg){
            nextProps.msg = this.props.msg;
            nextProps.error = this.props.error;
        }

        if(this.props.required){
            nextProps.required = this.props.required;
        }
    },
    render:function() {
        var data = this.state = this.props;
        data.type = data.type ? data.type : "text";
        return ~myInput;
    }
});
module.exports = myInputComponent;